<?php


class Champion extends Model {

    public $name;
    public $relative_href;
    public $stats;
    public $tooltip;
    public $icon;
    public $url;

    public $official_site = "http://www.bloodlinechampions.com/";

    public $abilities;

    public function __construct($container, $params = array()) {

        parent::__construct($container, $params);

        $this->url = $this->official_site.$this->relative_href;

    }


    public function recupera_abilita () {

        $html = file_get_contents($this->official_site.$this->relative_href);

        $doc = phpQuery::newDocument($html);

        phpQuery::selectDocument($doc);

        $this->abilities = array();

        $ex_count = 1;


        foreach(pq('div.ability_tooltip') as $key => $div) {


            //separiamo il testo dai dati del cooldown etc
            $abilities_data = pq($div)->find('p.ability_text:last > span:last')->eq(0)->html();

//
//           foreach (pq($div)->find('p.ability_text:last > span:last')->eq(0)->contents() as $node) {
//               echo "---> node type: ".$node->nodeType."<br/><br/>";
//               echo pq($node)->wrap("<div></div>")->html();
//
//           }

    //        echo $abilities_data."<br/>!!!!<br/>";

            //echo pq($div)->find('p.ability_text:last > span:last')->filter("span,br")->dumpTree();


            $abilities_data = "";

            foreach (pq($div)->find('p.ability_text:last > span:last')->eq(0)->contents() as $node) {

                if ($node->nodeType != 3) {
                    $abilities_data.= pq($node)->htmlOuter();
                }
            }

            //rimuovo a questo punto i dati tecnici
            pq($div)->find('p.ability_text:last > span:last > span, p.ability_text:last > span:last > br')->remove();

            $this->abilities[] = new Ability(
                    $this->container,
                    array (
                            'type' =>  pq($div)->find('h1.ability_type')->eq(0)->text(),
                            'name' => pq($div)->find('h1.ability_title')->eq(0)->text(),
                            'tooltip' => pq($div)->find('p.ability_text')->eq(0)->html(),
                            'specs' => $abilities_data,
                            'icon' => pq($div)->next("img")->attr("src"),
                            'index' => $key,
                            'champion' => $this
                    )

            );


            if (sizeof(pq($div)->find('h1.ability_type')) > 1) {


                $this->abilities[] = new Ability(
                        $this->container,
                        array (
                                'type' =>  pq($div)->find('h1.ability_type')->eq(1)->text(),
                                'name' => pq($div)->find('h1.ability_title')->eq(1)->text(),
                                'tooltip' => pq($div)->find('p.ability_text')->eq(1)->html(),
                                'specs' => $abilities_data,
                                'icon' => pq($div)->next("img")->attr("src"),
                                'shortcut' => "EX$ex_count",
                                'champion' => $this
                        )

                );

                $ex_count++;

            }
        }

    }

    public function save () {

        $sql = "insert into av_bloodlines (name, type, tooltip, icon, url)
                values (
                '".addslashes(stripslashes($this->name))."',
                '".addslashes(stripslashes($this->type))."',
                '".addslashes(stripslashes($this->tooltip))."',
                '".addslashes(stripslashes($this->icon))."',
                '".addslashes(stripslashes($this->url))."'
                )";

        $this->db->query ($sql);

        $this->id = $this->db->insert_id();

        foreach ($this->abilities as $ability) {
            $ability->save();
        }

    }

    public static function get_by_name ($container, $name) {
        $sql = "
            select 
            b.*
            from av_bloodlines b 
            where 
            b.name = '".addslashes(stripslashes($name))."'"
        ;

        $index = $container->db->query($sql);

        if ($record = $container->db->get_record($index)) {
            return new self($container, $record);
        }

        return null;
    }

    public static function get_by_id ($container, $id) {
        $sql = "
            select
            b.*
            from av_bloodlines b
            where
            b.id = '".$id."'"
        ;

        $index = $container->db->query($sql);

        if ($record = $container->db->get_record($index)) {
            return new self($container, $record);
        }

        return null;
    }


}